package cn.com.ege.mvc.action.business;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.ege.mvc.controller.BaseAction;
import cn.com.ege.mvc.controller.IAction;
import cn.com.ege.mvc.exception.BusinessException;
import cn.com.ege.mvc.model.business.Attribute;
import cn.com.ege.mvc.model.business.Category;
import cn.com.ege.mvc.model.business.Rule;
import cn.com.ege.mvc.service.impl.business.AttributeService;
import cn.com.ege.mvc.service.impl.business.CategoryService;
import cn.com.ege.mvc.service.impl.business.RuleService;
import cn.com.ege.mvc.util.PageController;

public class RuleAction extends BaseAction implements IAction, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4963006075956394620L;
	private final static Log logger = LogFactory.getLog(RuleAction.class);
	private PageController pager = null;
	private Rule rule = null;
	private RuleService ruleService = null;
	private List<Rule> ruleList = null;
	
	public String execute(HttpServletRequest request,
			HttpServletResponse response, ServletConfig servletConfig) {
		
		String act = request.getParameter("act");
		if(null != act){
			// 取列表
			if("list".equals(act)){
				int totalRows = ruleService.getRecordCount(new Rule());
				String URL = request.getRequestURI();
				this.pager.setURL(URL);
				this.pager.setTotalRowsAmount(totalRows);
				ruleList = ruleService.list(new Rule(), this.pager
						.getPageStartRow(), pager.getPageSize(),
						"order by id desc", true);
				pager.setData(ruleList);
				return getReturnURL(LIST);
			}
			// 点了添加或点了修改
			else if("get".equals(act)){
				rule = ruleService.get(rule);
				return getReturnURL(EDIT);
			}
			// 保存表单
			else if("save".equals(act)){
				try {
					ruleService.save(rule);
					return getReturnURL("getList");
				} catch (BusinessException e) {
					// 保存原来表单已输入的内容
					request.setAttribute("rule", rule);
					addMessage(e.getMessage());
					return getReturnURL(EDIT);
				}
			}
			// 删除
//			else if("del".equals(act)){
//				
//			}
			// 改变规则的状态
			else if("change".equals(act)){ 
				try {
					rule = ruleService.get(rule);
					rule.setStatus(Long.parseLong(request.getParameter("status")));
					ruleService.save(rule);
					return getReturnURL("getList");
				} catch (BusinessException e) {
					addMessage(e.getMessage());
				}
			}
			// 修订规则或初始化规则
			else if("establish".equals(act)){
				rule = ruleService.get(rule);
				return getReturnURL("establish");
			}
			// 初始化规则细则，类别和属性
			else if("saveCat".equals(act)){
				String rule_id = request.getParameter("rule_id");
				String cat = request.getParameter("cat_p");
				if(null != cat && !"".equals(cat)){
					String[] name = cat.split(",");
					for (int i = 0; i < name.length; i++) {
						try {
							new CategoryService().save(new Category(rule_id,name[i]));
						} catch (BusinessException e) {
							e.printStackTrace();
						}
					}
				}
				String att = request.getParameter("att_p");
				if(null != att && !"".equals(att)){
					List<Category> catList = new CategoryService().list(new Category(), null, null, "and rule_id = " + rule_id, true);
					String[] tmp = att.split("&");
					for (int i = 0; i < tmp.length; i++) {
						if(!"".equals(tmp[i])){
							String[] tmp2  = tmp[i].split("/");
							for (int j = 0; j < tmp2.length; j++) {
								String[] key_value = tmp2[j].split(",");
								try {
									if(2 == key_value.length)
										new AttributeService().save(new Attribute(catList.get(i).getId(),key_value[0],key_value[1]));
								} catch (BusinessException e) {
									e.printStackTrace();
								}
							}
						}
					}
				}
				// 改变规则状态为已初始化
				Rule rule = new Rule();
				rule.setId(Long.parseLong(rule_id));
				rule = ruleService.get(rule);
				rule.setStatus(1L);
				try {
					ruleService.save(rule);
				} catch (BusinessException e) {
					e.printStackTrace();
				}
				return getReturnURL("getList");
			}
			// 查看规则
			else if("check".equals(act)){
				rule = ruleService.get(rule);
				List<Category> catList = new CategoryService().list(new Category(), null, null, "and rule_id = " + rule.getId(), true);
				List<List<?>> attList = new ArrayList<List<?>>();
				for (int i = 0; i < catList.size(); i++) {
					attList.add(new AttributeService().list(new Attribute(), null, null, "and category_id = " + catList.get(i).getId(), true));
				}
				request.setAttribute("catList", catList);
				request.setAttribute("attList", attList);
				return getReturnURL("check");
			}
		}
		return null;
	}

	public PageController getPager() {
		return pager;
	}

	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public Rule getRule() {
		return rule;
	}

	public void setRule(Rule rule) {
		this.rule = rule;
	}

	public RuleService getRuleService() {
		return ruleService;
	}

	public void setRuleService(RuleService ruleService) {
		this.ruleService = ruleService;
	}
}
